#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "gs.h"
#include "tank.h"
#include "oilling.h"

static FILE* add_new_file(const char *filename)
{
    FILE* fp;
    /*
     * Check current directory filelist, if the list more than 1000 delete oldest
     */

    fp = fopen(filename, "a+");

    return fp;
}

/*
 * param mode: same as access
 */
static FILE* gs_oilling_get_fp(int mode)
{
    time_t now_t;
    struct tm* tm;
    FILE *fp;
    char filename[128];

    time(&now_t);
    tm = localtime(&now_t);

    /* Get current date as filename, it's format is code_alarm_yyyymmdd.txt */
    sprintf(filename, "./oilling/%s_oilling_%4d%02d%02d.txt", 
            g_gs.code, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);

    /* Check file exist in ./alarm directory 
     * if exist, open and return it, otherwize, add new file
     */
    if (access(filename, mode) == 0) {
        if (mode == R_OK)
            fp = fopen(filename, "r");
        else if (mode == W_OK)
            fp = fopen(filename, "a");
    }
    else {
        fp = add_new_file(filename);
    }

    return fp;
}

static void gs_oilling_save(void)
{
    int i;
    GS_TANK *tank;
    struct tm *tm1, *tm2;
    FILE *fp = NULL;
    

    for (i=0;i<g_gs.tanks_nr;i++) {

        tank = &g_gs.tanks[i];

        if (tank->state == 1) {
            if (fp == NULL)
                fp = gs_oilling_get_fp(W_OK);

            tm1 = localtime(&tank->begin_time);
            tm2 = localtime(&tank->end_time);
            fprintf(fp, "%s\t" // Code
                    "%d\t" // Tank ID
                    "%04d%02d%02d\t" // Date
                    "%02d%02d%02d\t" // tm1
                    "%.1f\t" // begin
                    "%02d%02d%02d\t" // tm1
                    "%.1f\t" // end
                    "%.1f\t", // add
                    g_gs.code,
                    tank->tank_id,
                    tm1->tm_year + 1900, tm1->tm_mon + 1, tm1->tm_mday,
                    tm1->tm_hour, tm1->tm_min, tm1->tm_sec,
                    tank->begin_oil,
                    tm2->tm_hour, tm2->tm_min, tm2->tm_sec,
                    tank->end_oil,
                    tank->end_oil - tank->begin_oil);
        }
    }

    if (fp)
        fclose (fp);
}

int gs_oilling_begin (void)
{
    int i;
    time_t now_t;
    GS_TANK *tank;
    time(&now_t);

    for (i=0; i<g_gs.tanks_nr;i++) {
        tank = &g_gs.tanks[i];
        tank->begin_time = now_t;
        tank->begin_oil = tank->oil;
    }

    return 0;
}

int gs_oilling_end (void)
{
    int i;
    time_t now_t;
    GS_TANK *tank;

    time(&now_t);

    for (i=0; i<g_gs.tanks_nr;i++) {
        tank = &g_gs.tanks[i];
        tank->end_time = now_t;
        tank->end_oil = tank->oil;

        if (tank->end_oil - tank->begin_oil > 1)
            tank->state = 1;
        else
            tank->state = 0;
    }

    gs_oilling_save();

    return 0;
}

int gs_oilling_ftp(void)
{
    /* Get new file from alarm index file */

    /* Send file to ftp server */

    return 0;
}

int gs_oilling_init(void)
{
    mkdir("oilling", 00777);

    return 0;
}
